﻿using JNPF.Basics.Models;
using JNPF.Utils;
using System;
using System.Threading.Tasks;

namespace JNPF.Basics.Logic
{
    /// <summary>
    /// 消息发送
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2018-08-28 
    /// </summary>
    public class QYMessageBll
    {
        private QYMessageIService service = new QYMessageService();
        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="requestParam">请求参数</param>
        /// <returns></returns>
        public async Task<PageResult<QYMessageEntity>> GetList(PageInput input)
        {
            try
            {
                return await service.GetList(input);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 发送
        /// </summary>
        /// <param name="entity">实体对象</param>
        public async Task Sent(QYMessageEntity entity)
        {
            var config = await new SysConfigBll().GetInfo("QYHConfig");
            var filePath = ConfigurationKey.MPMaterialFilePath + entity.FileJson;
            var touser = entity.All == 1 ? "@all" : entity.ToUserId.Replace(",", "|");//成员
            var result = true;
            try
            {

                //上传
                var mediaId = entity.MsgType == 1 ? "" : new WeChatQYHelper(config.QYH_CorpId, config.QYH_CorpSecret, config.QYH_AgentSecret).Upload(entity.MsgType.ToInt(), filePath);
                switch (entity.MsgType)
                {
                    case 1://文本
                        result = new WeChatQYHelper(config.QYH_CorpId, config.QYH_CorpSecret, config.QYH_AgentSecret).SendText(config.QYH_AgentId, entity.TXTContent, touser);
                        break;
                    case 2://图片
                        result = new WeChatQYHelper(config.QYH_CorpId, config.QYH_CorpSecret, config.QYH_AgentSecret).SendImage(config.QYH_AgentId, mediaId, touser);
                        break;
                    case 3://语音
                        result = new WeChatQYHelper(config.QYH_CorpId, config.QYH_CorpSecret, config.QYH_AgentSecret).SendVoice(config.QYH_AgentId, mediaId, touser);
                        break;
                    case 4://视频
                        result = new WeChatQYHelper(config.QYH_CorpId, config.QYH_CorpSecret, config.QYH_AgentSecret).SendVideo(config.QYH_AgentId, mediaId, touser);
                        break;
                    case 5://文件
                        result = new WeChatQYHelper(config.QYH_CorpId, config.QYH_CorpSecret, config.QYH_AgentSecret).SendFile(config.QYH_AgentId, mediaId, touser);
                        break;
                    case 6://图文
                        QYMpNewsModel mpNews = new QYMpNewsModel();
                        mpNews.author = entity.Author;
                        mpNews.title = entity.Title;
                        mpNews.content_source_url = entity.ContentSourceUrl;
                        mpNews.digest = entity.Abstract;
                        mpNews.content = entity.Content;
                        mpNews.thumb_media_id = mediaId;
                        result = new WeChatQYHelper(config.QYH_CorpId, config.QYH_CorpSecret, config.QYH_AgentSecret).SendMpNews(config.QYH_AgentId, mpNews, touser);
                        break;
                    default:
                        break;
                }
                entity.Id = CommonHelper.Guid;
                entity.SortCode = CommonHelper.SortCode;
                entity.SendDate = DateTime.Now;
                entity.SendUserId = UserProvider.Instance.Get().UserId;
                entity.AgentId = config.QYH_AgentId;
                entity.EnabledMark = result == true ? 1 : 0;
                await service.Create(entity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
    }
}
